<?php
/**
 * 手机验证码
 */

namespace app\api\model;
use think\Model;

use app\common\model\SmsDemo;
use app\common\model\SmsTencent;

class TelCode extends Base
{
    
    /**
    * 
    */
    
    /**
    * 校验验证码
    * @param tel string 手机号
    * @param code string 验证码
    * @param type int 类型 1注册 2登陆 3忘记密码
    */
    public function _checkCode($tel, $code, $type = 1){
        $where = [
            ['tel', '=', $tel],
            ['code', '=', $code],
            ['type', '=', $type],
        ];


        
        $data = $this->where($where)->order("created desc")->find();
        if(empty($data)){
            $this->error = '验证码错误';
            return false;
        }
        if($data['status'] != 1){
            $this->error = '验证码失效,请重试';
            return false;
        }
        
//        if($data['deadline'] < time()){
//            $this->error = '验证码失效,请重试';
//            return false;
//        }
        
        $data->status = 2;
        $ret = $data->save();
        if(!$ret){
            $this->error = '验证失败';
            return false;
        }
        
        return true;
    }
    
    /**
    * 发送短信
    * @param tel string 手机号
    * @param sign_name string 阿里云短信签名名称
    * @param code_num string 阿里云短信模板CODE
    * @param code string 验证码
    * @param type int 类型 1登录（绑定手机号）
    */
    public function _sendMsg($tel, $sign_name, $code_num, $code, $type = 1){
        $config = $this->getConfig();
        $deadline = time() + $config['SHORT_MSG_CYCLE'];
        $is_send = (int)$config['SHORT_MSG_OPEN'];
        if($is_send == 0){
            $code = '123456';
        }
        $where = [
            ['tel', '=', $tel],
            ['code', '=', $code],
            ['type', '=', $type],
            ['status', '=', 1],
            ['deadline', '>=', time()],
        ];
        $data = $this->where($where)->find();
        
        $channel = 1; //短信渠道 1阿里云，2腾讯云
        /*if(1 == $channel){
            //开启发送短信，
            $model = new SmsDemo;
            $ret = $model->sendSms($tel,$sign_name, $code_num,$code);
            if(!$ret){
                $this->error = '短信发送失败';
                return false;
            }
        }elseif(2 == $channel){
            $model = new SmsTencent;
            $ret = $model->_sendSignleBrief($tel, $code);
            if(!$ret){
                $this->error = '短信发送失败';
                return false;
            }
        }*/
        
        if(empty($data)){
            //短信失效或不存在，需写入短信库
            if($is_send == 1){
                $channel = 1; //短信渠道 1阿里云，2腾讯云
                if(1 == $channel){
                    //开启发送短信，
                    $model = new SmsDemo;
                    $ret = $model->sendSms($tel,$sign_name, $code_num,$code);
                    if(!$ret){
                        $this->error = '短信发送失败';
                        return false;
                    }
                }elseif(2 == $channel){
                    $model = new SmsTencent;
                    $ret = $model->_sendSignleBrief($tel, $code);
                    if(!$ret){
                        $this->error = '短信发送失败';
                        return false;
                    }
                }
                
                

                $insert_data = [
                    'tel' => $tel,
                    'code' => $code,
                    'type' => $type,
                    'deadline' => $deadline,
                    'created' => time()
                ];
                
                $ret1 = $this->save($insert_data);
                if(!$ret){
                    $this->error = '短信发送失败';
                    return false;
                }
            }else{
                $insert_data = [
                    'tel' => $tel,
                    'code' => $code,
                    'type' => $type,
                    'deadline' => $deadline,
                    'created' => time()
                ];
                
                $ret = $this->save($insert_data);
                if(!$ret){
                    $this->error = '短信发送失败';
                    return false;
                }
            }
        }
        
        return true;
        
    }
    

}